//
//  ChooseGoodView.m
//  自定义商品选择窗口
//
//  Created by 孙鹏飞 on 2017/6/26.
//  Copyright © 2017年 homai. All rights reserved.
//
#define topHeight 250 
#define titleHeight 50
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ChooseGoodView.h"
#import "ChooseGoodCell.h"
#import "ChooseGoodModel.h"

@interface ChooseGoodView() <UITableViewDelegate, UITableViewDataSource>

/** 弹窗视图 */
@property (nonatomic, weak) UIView * alertView;
/** 标题 */
@property (nonatomic, weak) UIButton * titleBtn;
/** 确认按钮 */
@property (nonatomic, weak) UIButton * actionBtn;
/** 分割线 */
@property (nonatomic, weak) UIView * lineView;
/** 弹窗表格 */
@property (nonatomic, weak) UITableView * tableView;

// 模型数组
@property (nonatomic, strong) NSMutableArray * itemArray;
// 记录用户选中行的索引
@property (nonatomic, strong) NSMutableArray * selectedArray;

@end

@implementation ChooseGoodView

- (NSMutableArray *)itemArray
{
    if (_itemArray == nil) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (NSMutableArray *)selectedArray
{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        [self setUPSubview];
    }
    return self;
}

- (void)setOid:(NSString *)oid
{
    _oid = oid;
    if (self.itemArray.count == 0) {
        [HttpBaseTool get:kConfirmOrder params:@{@"uid":[MGAccountTool getUid], @"oid":self.oid} success:^(id responseObj) {
            self.itemArray = [ChooseGoodModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"][@"list"]];
            [self.tableView reloadData];
        } failure:^(NSError *error) {
            
        }];
    }
}



- (void)setUPSubview
{
    UIView * alertView = [[UIView alloc] initWithFrame:CGRectMake(0, topHeight, WIDTH, HEIGHT - topHeight)];
    alertView.backgroundColor = [UIColor whiteColor];
    self.alertView = alertView;
    [self addSubview:_alertView];
    
    UIButton * titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    titleBtn.frame = CGRectMake(0, 0, WIDTH - 60, titleHeight - 1);
    titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [titleBtn setTitle:@"请选择退货商品" forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.titleBtn = titleBtn;
    [alertView addSubview:titleBtn];
    
    UIButton * actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    actionBtn.frame = CGRectMake(WIDTH - 60, 0, 60, titleHeight - 1);
    actionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [actionBtn setTitle:@"完成" forState:UIControlStateNormal];
    [actionBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.actionBtn = actionBtn;
    [actionBtn addTarget:self action:@selector(actionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [alertView addSubview:actionBtn];
    
    
    UIView * lineView = [[UIView alloc] init];
    lineView.frame = CGRectMake(0, titleHeight - 1, WIDTH, 1);
    lineView.backgroundColor = [UIColor lightGrayColor];
    self.lineView = lineView;
    [alertView addSubview:lineView];
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, titleHeight, WIDTH,alertView.frame.size.height - titleHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [alertView addSubview:_tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ChooseGoodCell class]) bundle:nil] forCellReuseIdentifier:@"ChooseGoodCell"];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseGoodCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseGoodCell"];
    cell.model = self.itemArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseGoodModel * model = self.itemArray[indexPath.row];
    
    if (model.isChecked) {
        model.checked = NO;
        [self.selectedArray removeObject:model];
    } else {
        model.checked = YES;
        [self.selectedArray addObject:model];
    }
    [self.tableView reloadData];
    
}

- (void)show
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    self.alpha = 0;
    self.alertView.transform = CGAffineTransformMakeScale(1.08, 1.08);
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
        self.alertView.transform = CGAffineTransformIdentity;
    }];
}

- (void)dismiss
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    }completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)actionBtnClick
{
    if (_actionBlock) {
        self.actionBlock(self.selectedArray);
    }
    [self dismiss];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}


@end
